/**
 * 
 */
package com.excalibur.core.util;

import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.UUID;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.SecretKeySpec;

import org.apache.commons.codec.binary.Base64;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author "Jiance Qin"
 * 
 * @date 2014年3月7日
 * 
 * @time 下午4:31:11
 * 
 * @desc
 * 
 */
public class EncryptUtils {

	protected static final Logger logger = LoggerFactory.getLogger(EncryptUtils.class);

	public static final String DEFAULT_CHARSET = "UTF-8";

	/**
	 * 
	 */
	public EncryptUtils() {

	}

	/**
	 * 生成UUID.
	 * 
	 * @return
	 */
	public static String getUUID() {
		String uuid = UUID.randomUUID().toString();
		StringBuilder sb = new StringBuilder();
		sb.append(uuid.substring(0, 8));
		sb.append(uuid.substring(9, 13));
		sb.append(uuid.substring(14, 18));
		sb.append(uuid.substring(19, 23));
		sb.append(uuid.substring(24));
		return sb.toString();
	}

	/**
	 * @param securityKey
	 * @param content
	 * @return
	 */
	public static String defaultAESEncrypt(String securityKey, String content) {
		try {
			// create key
			SecretKeySpec key = new SecretKeySpec(securityKey.getBytes(DEFAULT_CHARSET), "AES");

			// create cipher
			Cipher cipher = Cipher.getInstance("AES");
			byte[] byteContent = content.getBytes(DEFAULT_CHARSET);
			cipher.init(Cipher.ENCRYPT_MODE, key);

			// encryption
			byte[] result = cipher.doFinal(byteContent);

			return defaultBase64EncodeAsString(result);
		} catch (NoSuchAlgorithmException e) {
			logger.error("NoSuchAlgorithmException.", e);
		} catch (NoSuchPaddingException e) {
			logger.error("NoSuchPaddingException.", e);
		} catch (InvalidKeyException e) {
			logger.error("InvalidKeyException.", e);
		} catch (UnsupportedEncodingException e) {
			logger.error("UnsupportedEncodingException.", e);
		} catch (IllegalBlockSizeException e) {
			logger.error("IllegalBlockSizeException.", e);
		} catch (BadPaddingException e) {
			logger.error("BadPaddingException.", e);
		}
		return null;
	}

	/**
	 * @param securityKey
	 * @param content
	 * @return
	 */
	public static String defaultAESDecrypt(String securityKey, byte[] content) {
		try {
			SecretKeySpec key = new SecretKeySpec(securityKey.getBytes(DEFAULT_CHARSET), "AES");
			Cipher cipher = Cipher.getInstance("AES");// 创建密码器
			cipher.init(Cipher.DECRYPT_MODE, key);// 初始化
			byte[] result = cipher.doFinal(content);
			return new String(result);
		} catch (NoSuchAlgorithmException e) {
			logger.error("NoSuchAlgorithmException.", e);
		} catch (NoSuchPaddingException e) {
			logger.error("NoSuchPaddingException.", e);
		} catch (InvalidKeyException e) {
			logger.error("InvalidKeyException.", e);
		} catch (IllegalBlockSizeException e) {
			logger.error("IllegalBlockSizeException.", e);
		} catch (BadPaddingException e) {
			logger.error("BadPaddingException.", e);
		} catch (UnsupportedEncodingException e) {
			logger.error("UnsupportedEncodingException.", e);
		}
		return null;
	}

	/**
	 * @param salt
	 * @param content
	 * @return
	 */
	public static String saltSHA256(String salt, String content) {
		try {
			MessageDigest md = MessageDigest.getInstance("SHA-256");
			md.reset();
			md.update(salt.getBytes(DEFAULT_CHARSET));
			byte[] result = md.digest(content.getBytes(DEFAULT_CHARSET));
			return defaultBase64EncodeAsString(result);
		} catch (Exception e) {
			logger.error("Exception.", e);
		}
		return null;
	}

	/**
	 * @param content
	 * @return
	 */
	public static byte[] base64Decode(String content) {
		try {
			return Base64.decodeBase64(content.getBytes(DEFAULT_CHARSET));
		} catch (UnsupportedEncodingException e) {
			return Base64.decodeBase64(content);
		}
	}

	/**
	 * @param content
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	public static String defaultBase64Encode(String content) throws UnsupportedEncodingException {
		return defaultBase64EncodeAsString(content.getBytes(DEFAULT_CHARSET));
	}

	/**
	 * @param content
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	public static String defaultBase64EncodeAsString(byte[] content) throws UnsupportedEncodingException {
		return new String(defaultBase64Encode(content), DEFAULT_CHARSET);
	}

	/**
	 * @param content
	 * @return
	 */
	public static byte[] defaultBase64Encode(byte[] content) {
		return Base64.encodeBase64(content);
	}

}
